/*
	新增经销商
*/
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

//dom
$(function(){
	layui.use(['form', 'layer'], function(){
		var form = layui.form, layer = layui.layer;
		var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken };

		function addNewAgent(agentData){
			/*
				经销商新增算法
				@agentData 新增的经销商数据
			*/
			loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			var  _ajaxData = $.extend({}, agentData, ajaxData);
			$.ajax({
				url: BASE_SERVICEURL + URlS.agent,
				type: 'PUT',
				data: _ajaxData,
				success: function(responseData) {
					layer.close(loadIndex);
					if(responseData.msg.code == 200){
						layer.msg(responseData.msg.msg, {time: 2000, end: function(){
							parent.location.reload();
						}})
					}else{
						layer.msg(responseData.msg.msg, {time: 2000});
					};
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						window.location.href = './500.html';
					}});
				}
			})
		}
		form.on('submit(add)', function(data){
			/*
				提交新增表单
				@ data layui form 数据
			*/
			layer.confirm(MESSAGES.confirm_add, {title: '新增提示'}, function(index){
				var f = data.field;
				var md = {
					code: f.agtCode,
					name: langTransObj(f.agtNameCn, f.agtNameTw, f.agtNameEn),
					intro: langTransObj(f.descCn, f.descTw, f.descEn),
					addr: langTransObj(f.addrCn, f.addrTw, f.addrEn),
					phone: f.agtPhone,
					coordinates: f.lat + '|' + f.lng,
					time: f.openTime,
					status: f.status,
					fax: "----",
					img: f.agentImgLink,
					merge: langTransObj(f.agtMergeCn, f.agtMergeTw, f.agtMergeEn)
				};
				layer.close(index);
				addNewAgent(md);
			});
			return false;
		});

		jQuery('button[name="reload"]').on('click', function(){
			/*
				重置表格
			*/
			layer.confirm('确认重置数据？（数据全部丢失）？', function(index){
				window.location.reload();
				layer.close(index)
			});
		});


	})
})











